﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Soufun.DB;
using Soufun.Model;

namespace Soufun.DAL
{
    public class DProjectUrl
    {
        public int Add(ProjectUrlInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Insert into tprojectUrlinfo (");
            strSql.Append("ProjectName,ProjectUrl)");
            strSql.Append(" value (");
            strSql.Append("@0,@1)");
            object[] paramaters = new object[]
                {                     
                    model.ProjectName,
                    model.ProjectUrl,                    
                };
            int result = DbHelper.ExecuteSql(strSql.ToString(), paramaters);
            if (result==0)
            {
                return -1;
            }
            DataSet ds = GetList("ProjectName='" + model.ProjectName +
                                 "' and ProjectUrl='" + model.ProjectUrl + "'");
            if (ds!=null && ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
            {
                int id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                return id;
            }

            return -1;
        }

        public string ExistUrl(string url)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProjectName from tprojectUrlinfo ");
            strSql.Append("where ProjectUrl='");
            strSql.Append(url);
            strSql.Append("'");
            DataSet ds = DbHelper.Query(strSql.ToString());
            if (ds==null || ds.Tables.Count ==0 || ds.Tables[0].Rows.Count==0)
            {
                return "";
            }
            return ds.Tables[0].Rows[0][0].ToString();
        }


        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tprojectUrlinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }            
            return DbHelper.Query(strSql.ToString());
        }

        public int DeleteById(int projectId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tprojectUrlinfo ");
            strSql.Append("where Id=");
            strSql.Append(projectId);
            int result = DbHelper.ExecuteSql(strSql.ToString());
            return result;
        }
    }
}
